// pages/postlost/postlost.js

import API from "../../data/api";
import Util from "../../utils/util";

const App = getApp();
const Config = App.Config;

Page({
  data: {

    selectImg: true, // 是否超过3张图片

    arrayImg: [], // 图片数组
    keysImg: [], // 图片KEY
    disableSend: false,

    arrayType: ['寻物启事', '二手闲置', '吐槽专区'], // 类型选择
    indexType: 0, // 类型选择
    showContact: true,

    arrayContact: ['手机', '微信'], // 联系方式选择
    indexContact: 0, // 联系方式选择
    content: '', // 内容
    contact: '', // 联系方式


    HOSTIMG: Config.HOSTIMG, // 图片
  },

  onShow: function () {
    if (App.globalData.openId == '' || App.globalData.openId == null) {
      Util.UI.loading(1000, '授权失败,请退出小程序重新登录');
      setTimeout(function () {
        wx.switchTab({
          url: '../index/index',
        })
      }, 1000)
    }
  },

  // 内容
  contentInput: function (event) {
    this.setData({ content: event.detail.value });
  },

  // 联系方式
  contactInput: function (event) {
    this.setData({ contact: event.detail.value });
  },

  // 选择联系方式
  bindPickerContactChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      indexContact: e.detail.value
    })
  },

  // 选择类型
  bindPickerTypeChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    var that = this;
    if (e.detail.value == 2) {
      that.setData({
        indexType: e.detail.value,
        showContact: false
      })
    } else {
      that.setData({
        indexType: e.detail.value,
        showContact: true
      })
    }
  },

  // 删除图片
  delCoverImg: function (event) {
    // console.log(event.currentTarget.dataset.id);
    var that = this;
    var id = event.currentTarget.dataset.id;
    var arrayData = that.data.arrayImg;
    var arraykeysData = that.data.keysImg;
    arrayData.splice(id, 1);
    arraykeysData.splice(id, 1);
    that.setData({
      arrayImg: arrayData,
      keysImg: arraykeysData,
    })
    if (that.data.arrayImg.length < 3) {
      that.setData({
        selectImg: true
      })
    }
  },

  // 添加图片
  addCoverImg: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        var arrayData = that.data.arrayImg;
        arrayData.push(...res.tempFilePaths);
        that.setData({
          arrayImg: arrayData,
          disableSend: true,
        });
        if (that.data.arrayImg.length == 3) {
          that.setData({
            selectImg: false
          })
        }
        that.uploadFile(res.tempFilePaths[0]);
      }
    })
  },

  // 发送按钮
  sendBtnClick: function () {
    var that = this;
    var i;

    // 判断数据是否完整
    if (that.data.content == "" || (that.data.contact == "" && that.data.indexType != 2)) {
      wx.showModal({
        title: '提示',
        content: '还有内容没有填写哦~',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
          }
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '确定上传，资料无法修改哦~',
        success: function (res) {
          if (res.confirm) {
            API.postCommunity(App.globalData.openId, App.globalData.userInfo.nickName, App.globalData.userInfo.avatarUrl, that.data.content, that.data.keysImg, that.data.arrayContact[that.data.indexContact], that.data.contact, parseInt(that.data.indexType))
              .then((res) => {
                // console.log(res.data.error == Config.ERR.OK);
                if (res.data.error == Config.ERR.OK) {
                  Util.UI.toast(res.data.data);
                  setTimeout(function () {
                    wx.redirectTo({
                      url: '../community/community',
                    })
                  }, 1000)
                } else {
                  Util.UI.loading(1000, res.data.data);
                }
              }, (res) => {
                Util.UI.loading(1000, 'error');
              });
          }
        }
      })

    }
  },

  // 上传图片
  uploadFile: function (img) {
    var that = this;
    API.uploadFile(img)
      .then((res) => {
        // console.log(res);
        var obj = JSON.parse(res.data);
        if (obj.error == Config.ERR.OK) {
          // console.log(obj.data);
          var newData = that.data.keysImg;
          newData.push(obj.data);
          // console.log(newData);
          that.setData({
            keysImg: newData,
            disableSend: false,
          })
        }
      });
  }


})